package com.chj.comp.common.help.util;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.util.Scanner;

/**
 * -mp代码生成工具类，使用freemarker引擎
 * -使用前可以进行的修改
 *      gc.setAuthor  设置作者，会出现在类前
 *      gc.setOutputDir 设置输出路径，默认为 D:\
 *      pc.setParent 设置输出的包名(也即文件夹名)
 *      DataSourceConfig 设置数据源
 * -实体类使用lombok,swagger2，驼峰命名，去除is_前缀
 * -controller 使用rest风格
 * -mapper.xml生成resultMap和baseColumnList
 * @author cuihui
 * @create 2021-09-17 14:51
 */
public class CodeGeneratorUtil {
    /**
     * 代码生成位置
     */
    private static String generatePath = System.getProperty("user.dir") + "\\code-generate";
    /**
     * 包路径
     */
    private static String packagePath = "com.maam.grid";
//    private static String packagePath = "com.scbb.activity";
//    private static String packagePath = "com.zdww.blmp.community";
//    private static String packagePath = "com.chj.comp.service.demo2";

    /**
     * 数据库信息
     */
    private static String dbDriverName = "com.mysql.jdbc.Driver";

//    private static String dbUrl = "jdbc:mysql://10.18.100.102:3306/blmp_by?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false";
//    private static String dbUserName = "root";
//    private static String dbPassword = "Blmp2020!";

//    private static String dbUrl = "jdbc:mysql://10.18.104.126:3306/icity?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&allowPublicKeyRetrieval=true";
//    private static String dbUserName = "root";
//    private static String dbPassword = "Icity2012!";

//    private static String dbUrl = "jdbc:mysql://192.168.153.101:3306/spider_data?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false";
//    private static String dbUserName = "root";
//    private static String dbPassword = "since2012";

//    private static String dbUrl = "jdbc:mysql://192.168.6.244:31133/sc_base_support?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false";
//    private static String dbUserName = "root";
//    private static String dbPassword = "p9nk28vgtnhk0dim";

    private static String dbUrl = "jdbc:mysql://10.18.100.207:3306/sc_grid_management_OQLQ?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false";
    private static String dbUserName = "scity";
    private static String dbPassword = "RBhx9qB4pyNLlmQZ";

    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {
        AutoGenerator mpg = new AutoGenerator();
        // 选择 freemarker 引擎，默认 Veloctiy
//        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        //路径,默认为 D:\
        File file = new File(generatePath);
        if(!file.exists()){
            file.mkdir();
        }
        gc.setOutputDir(generatePath);
        // 文件覆盖
        gc.setFileOverride(true);
        // 不需要ActiveRecord(实体类继承Model)特性的请改为false
        gc.setActiveRecord(true);
        // XML 二级缓存
        gc.setEnableCache(false);
        // XML ResultMap
        gc.setBaseResultMap(false);
        // XML column List
        gc.setBaseColumnList(false);
        //作者
        gc.setAuthor("cuihui");
        //使用swagger2
        gc.setSwagger2(true);
        //设置主键生成策略
//        gc.setIdType(IdType.ID_WORKER_STR);
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setDriverName(dbDriverName);
        dsc.setUsername(dbUserName);
        dsc.setPassword(dbPassword);
        dsc.setUrl(dbUrl);
        mpg.setDataSource(dsc);

//        //自定义controller模板
//        String templatePath = "/templates/controller.java.vm";
//        // 自定义输出配置
//        List<FileOutConfig> focList = new ArrayList<>();
//        // 自定义配置会被优先输出
//        focList.add(new FileOutConfig(templatePath) {
//            @Override
//            public String outputFile(TableInfo tableInfo) {
//                // 自定义输出文件名,如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化！！
//                return "E:\\Desktop\\代码生成工具\\com\\zdww\\blmp\\mission\\controller"
//                        + "/" + tableInfo.getEntityName() + "Controller" + StringPool.DOT_JAVA;
//            }
//        });

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        // strategy.setCapitalMode(true);// 全局大写命名 ORACLE 注意
        //实体类使用lombok
        strategy.setEntityLombokModel(true);
        //rest风格controller
        strategy.setRestControllerStyle(true);
        //is_xxx 去除is_ 前缀
        strategy.setEntityBooleanColumnRemoveIsPrefix(false);
        //字段注释
        strategy.setEntityTableFieldAnnotationEnable(true);
        //数据表命名策略
        strategy.setNaming(NamingStrategy.underline_to_camel);
        //数据表列生成策略
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        // 需要生成的表(不设置生成所有表)
        strategy.setInclude(scanner("表名，多个英文逗号分割").split(","));
        mpg.setStrategy(strategy);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(packagePath);
        mpg.setPackageInfo(pc);
        // 执行生成
        mpg.execute();
    }
}